<template>
  <el-main>
    <el-card class="box-card" shadow="never">
      <div class="search-box">
        <el-form
          :inline="true"
          :model="searchForm"
          class="demo-form-inline"
          label-width="80px"
        >
          <el-form-item label="星期天数">
            <el-select v-model="searchForm.week_day" placeholder="请选择天数">
              <el-option label="星期一" value="1"></el-option>
              <el-option label="星期二" value="2"></el-option>
              <el-option label="星期三" value="3"></el-option>
              <el-option label="星期四" value="4"></el-option>
              <el-option label="星期五" value="5"></el-option>
              <el-option label="星期六" value="6"></el-option>
              <el-option label="星期日" value="7"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" icon="el-icon-search"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" icon="el-icon-plus" @click="addWeekActivity"
        >添加限定</el-button
      >
      <div class="line"></div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID"> </el-table-column>
        <el-table-column label="类型">
          <template #default="scope">
            <el-tag v-if="scope.row.type == 1">必中现金</el-tag>
            <el-tag v-if="scope.row.type == 2">聚划算</el-tag>
            <el-tag v-if="scope.row.type == 3">N倍送</el-tag>
            <el-tag v-if="scope.row.type == 4">首单N元</el-tag>
            <el-tag v-if="scope.row.type == 5">加1元发送N</el-tag>
            <el-tag v-if="scope.row.type == 6">买N送N</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="week_day" label="星期几"></el-table-column>
        <el-table-column prop="title" label="活动标题"></el-table-column>
        <el-table-column prop="sub_title" label="副标题"></el-table-column>
        <el-table-column
          prop="buy_limit_num"
          label="限购次数"
        ></el-table-column>
        <el-table-column prop="create_time" label="创建日期"> </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button @click="handleDel(scope.row)" type="text" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 10px"
        background
        layout="->, prev, pager, next"
        :page-size="searchForm.limit"
        @current-change="handlePageChange"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <save-dialog
      v-if="dialog.save"
      ref="saveDialog"
      @success="handleSuccess"
      @closed="dialog.save = false"
    ></save-dialog>
  </el-main>
</template>
  
<script>
import saveDialog from "./save";

export default {
  name: "week_day",
  components: {
    saveDialog,
  },
  data() {
    return {
      dialog: {
        save: false,
      },
      searchForm: {
        week_day: "",
        limit: 15,
        page: 1,
      },
      total: 1,
      tableData: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 查询
    onSubmit() {
      this.searchForm.page = 1;
      this.getList();
    },
    // 翻页
    pageChangeHandle(page) {
      this.searchForm.page = page;
      this.getList();
    },
    // 获取领取记录
    async getList() {
      let res = await this.$API.week.list.get(this.searchForm);
      this.tableData = res.data.data;
      this.total = res.data.total;
    },
    // 添加每日限定
    addWeekActivity() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    // 更新成功
    handleSuccess() {
      this.getList();
    },
    // 编辑
    handleEdit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.setData(row).open("edit");
      });
    },
    // 删除
    handleDel(row) {
      this.$confirm("此操作将永久删除该广告, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await this.$API.week.del.get({ id: row.id });
          if (res.code == 0) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
  },
};
</script>
  
  <style scoped>
.primary {
  color: #fff;
  background-color: #409eff;
  border-color: #409eff;
}
.line {
  width: 100%;
  height: 0;
  border-bottom: #e4e7ed 1px dashed;
  margin-top: 15px;
}
</style>
  